#include "ls_http_post.h"
#include "ls_common.h"


#define DBG_TAG "HTTP"
#include "ls_trace_common.h"

#define HTTP_SERVER_IP   "47.113.146.156"
//#define HTTP_SERVER_IP   "192.168.6.106"
#define HTTP_SERVER_PORT 8001

//"Content-Length:16393\r\n"\
//"Transfer-Encoding: chunked\r\n"\

//
#define POST_BUFF_SIZE  1024
#define POST_BUFF_SIZE_MIN  512

#define LS_HTTP_OK "HTTP/1.1 200 OK"


#define LS_UPLOAD_IMAGE_HEADER() \
"POST /api/ajax/upload2 HTTP/1.1\r\n" \
"Host: 47.113.146.156:8001\r\n"\
"Server: true\r\n"\
"Transfer-Encoding: chunked\r\n"\
"Content-Type: multipart/form-data; boundary=AaB03x\r\n"\
"Accept: */*\r\n"\
"User-Agent: LS/1.0\r\n" \
"Connection: close\r\n\r\n"


#define LS_UPLOAD_BODY_START()\
"--AaB03x\r\n"\
"Content-Disposition: form-data; name=\"file\"; filename=\"%s.jpeg\"\r\n"\
"Content-Type: image/jpeg\r\n\r\n"


#define LS_UPLOAD_IMAGE_END \
"\r\n--AaB03x--\r\n"

 
#define LS_ALARM_MSG_HEADER() \
"POST /api/ajax/pushMsg HTTP/1.1\r\n" \
"Host: 47.113.146.156:8001\r\n" \
"Server: true\r\n" \
"Content-Type: application/json\r\n" \
"Transfer-Encoding: chunked\r\n"\
"Connection: close\r\n" \
"\r\n"

#define LS_ALARM_MSG_JSON "{\"deviceId\": \"%s\",\"type\":\"2\",\"img\": \"%s\",\"createTime\":\"%s\"}"




typedef struct 
{
    int   sock_client;
    char *file_path;
    char *send_buff;
    char *send_buff1;
    /* data */
}ls_http_s;

static ls_http_s  http_post={
    .sock_client = -1,
};



/// @brief 
/// @param filename 
/// @return 
int ls_http_post_img_start(const char *filename)
{
    LS_RETURN(http_post.sock_client == -1,-1,"http is start......");

    int sock_client = -1;
    struct sockaddr_in ipv4;
    ipv4.sin_family = AF_INET;
    ipv4.sin_port   = htons(HTTP_SERVER_PORT);
    ipv4.sin_addr.s_addr   = 0;

    inet_pton(AF_INET,HTTP_SERVER_IP,&ipv4.sin_addr); 
       
    //
    if ((sock_client = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        LS_TRACE_ERROR("Http Client create socket error");
        return -1;
    }
    //
    if (connect(sock_client, (struct sockaddr *)&ipv4, sizeof(struct sockaddr)) != 0) {
        LS_TRACE_ERROR("http client connect server falied!");
        closesocket(sock_client);
        return -1;
    }
    http_post.sock_client = sock_client;

    send(sock_client,LS_UPLOAD_IMAGE_HEADER(),strlen(LS_UPLOAD_IMAGE_HEADER()),0);

    http_post.send_buff = malloc(POST_BUFF_SIZE);
    memset(http_post.send_buff,0,POST_BUFF_SIZE);
    snprintf(http_post.send_buff,POST_BUFF_SIZE,LS_UPLOAD_BODY_START(),filename);

    char buffer[16]={0};
    sprintf(buffer,"%x\r\n",strlen(http_post.send_buff));
    send(http_post.sock_client,buffer, strlen(buffer),0);
    int ret =send(http_post.sock_client,http_post.send_buff,strlen(http_post.send_buff),0);
    send(http_post.sock_client,"\r\n", 2, 0);

}


/// @brief 
/// @param data 
/// @param datalen 
/// @return 
int ls_http_post_img_data(uint8_t *data,uint16_t datalen)
{
    LS_RETURN(http_post.sock_client != -1,-1,"http is not start......");

    char buffer[16]={0};
    sprintf(buffer,"%x\r\n",datalen);
    send(http_post.sock_client, buffer,strlen(buffer),0);
    int ret = send(http_post.sock_client,data,datalen,0);
    send(http_post.sock_client, "\r\n", 2, 0);
}


/// @brief 
/// @return 
int ls_http_post_img_end()
{
    usleep(1000);
    LS_RETURN(http_post.sock_client != -1,-1,"http is not start......");

    char buffer[16]={0};
    sprintf(buffer,"%x\r\n",strlen(LS_UPLOAD_IMAGE_END));
    send(http_post.sock_client, buffer, strlen(buffer),0);
    send(http_post.sock_client,LS_UPLOAD_IMAGE_END,strlen(LS_UPLOAD_IMAGE_END),0);
    send(http_post.sock_client,"\r\n", 2, 0);
    send(http_post.sock_client,"0\r\n",3, 0);
    send(http_post.sock_client,"\r\n", 2, 0);

    memset(http_post.send_buff,0,POST_BUFF_SIZE);
    int recvlen=recv(http_post.sock_client,http_post.send_buff,POST_BUFF_SIZE,0);
    if(recvlen > 0 && strstr(http_post.send_buff,"Transfer-Encoding: chunked") && strstr(http_post.send_buff,LS_HTTP_OK))
    {
        char *pstart=strstr(http_post.send_buff,"\r\n\r\n");
        if(pstart)
        {
            pstart+=4; 
            char *pend=strstr(pstart,"\r\n");
            char buff[8]={0};
            memcpy(buff,pstart,pend-pstart < 8 ? pend-pstart:8);
            uint16_t data_len = 0;
            sscanf(buff,"%x",&data_len);
            if(data_len < 512)
            {
               http_post.file_path =(char *)malloc(data_len+1);
               memset(http_post.file_path,0,data_len+1);
               memcpy(http_post.file_path,pend+2,data_len);
            }
        }
    }
    else
    {
        LS_TRACE_DEBUG("http_post.send_buff:%s",http_post.send_buff);
    }
    //
    closesocket(http_post.sock_client);
    http_post.sock_client = -1;
    free(http_post.send_buff);
    http_post.send_buff = NULL;
}


/// @brief 
/// @param deviceId 
/// @param filename 
/// @param time_str 
/// @return 
int ls_http_post_alarm_msg(const char *deviceId,const char *time_str)
{
    if(!http_post.file_path)
    {
        return -1;
        LS_TRACE_ERROR("img file name is error!");
    }

    int sock_client = -1;
    struct sockaddr_in ipv4;
    ipv4.sin_family = AF_INET;
    ipv4.sin_port   = htons(HTTP_SERVER_PORT);
    inet_pton(AF_INET,HTTP_SERVER_IP,&ipv4.sin_addr);
    //
    if ((sock_client = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        LS_TRACE_ERROR("Http Client create socket error");
        return -1;
    }
    //
    if (connect(sock_client, (struct sockaddr *)&ipv4, sizeof(struct sockaddr)) != 0) {
        LS_TRACE_ERROR("Http client connect server falied!");
        closesocket(sock_client);
        return -1;
    }
   
    //头
    http_post.send_buff1 = malloc(POST_BUFF_SIZE_MIN);

    write(sock_client,LS_ALARM_MSG_HEADER(),strlen(LS_ALARM_MSG_HEADER()));

    //body
    memset(http_post.send_buff1,0,POST_BUFF_SIZE_MIN);
    snprintf(http_post.send_buff1,POST_BUFF_SIZE_MIN,LS_ALARM_MSG_JSON,deviceId,http_post.file_path,time_str);


    char buffer[16]={0};
    sprintf(buffer,"%x\r\n",strlen(http_post.send_buff1));
    send(sock_client, buffer,strlen(buffer),0);  
    write(sock_client,http_post.send_buff1 ,strlen(http_post.send_buff1));
    send(sock_client, "\r\n0\r\n\r\n",7,0);

    memset(http_post.send_buff1,0,POST_BUFF_SIZE_MIN);
    int recvlen=recv(sock_client,http_post.send_buff1,POST_BUFF_SIZE_MIN,0);
    if(strstr(http_post.send_buff1,LS_HTTP_OK))
    {
        LS_TRACE_DEBUG("alarm msg success:%s!",http_post.file_path);
    }
    else
    {
        LS_TRACE_ERROR("alarm push msg error!");
    }
    
    free(http_post.file_path);
    free(http_post.send_buff1);
    closesocket(sock_client);
}